package actions;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.DegreeForm;
import model.StudentModel;

public class StudentAppAction extends Action {

	private static final boolean DEBUG = true;
	//private static final int UNKNOWN = -1;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
		
		String fname = "";
		String mname = "";
		String lname = "";
		String citizen = "";
		String residence = "";
		String address = "";
		String city = "";
		String state="";
		String zip_code = "";
		String country_code= "";
		String area_code = "";
		String telephone = "";
		String residency = "";
		String specialization = "";
		String admission = "";
		
		Vector<DegreeForm> degrees = new Vector<DegreeForm>();
		DegreeForm degree;
		InputStream transcript;
		
		String string_val = "";
		int string_id = 0;
		
		// Look up student app id
		int stu_id = (Integer) request.getSession().getAttribute("studentapp_id");
		
		// Look up student application record
		RowSet rset = StudentModel.getStudent(stu_id);
		
		try {
			while (rset.next()) {
				// student names
				fname = rset.getString("first_name");
				lname = rset.getString("last_name");
				mname = rset.getString("middle_name");			
				// citizenship
				citizen = StudentModel.getString(StudentModel.StringToID.COUNTRY, 
								rset.getInt("citizenship"));
				// residence
				residence = StudentModel.getString(StudentModel.StringToID.COUNTRY, 
						rset.getInt("residence"));
				// address
				address = rset.getString("street_addr");
				city = rset.getString("city");
				state = rset.getString("state");
				zip_code = String.valueOf(rset.getInt("zip_code"));
				country_code = String.valueOf(rset.getInt("country_code"));				
				area_code = String.valueOf(rset.getInt("area_code"));
				telephone = String.valueOf(rset.getInt("telephone"));
				// residency
				residency = String.valueOf(rset.getBoolean("residency"));	
				// specialization
				specialization = StudentModel.getString(StudentModel.StringToID.SPECIAL, 
						rset.getInt("specialization"));
				admission = rset.getString("admission_status");
				
				if(DEBUG) {
					System.err.println("StudentAppAction: student found");
				}
			}
		} catch (SQLException e) {
			System.err.println("SQLException: " + e.getMessage());
		}
		
		rset = StudentModel.getDegrees(stu_id);
		
		try {
			while (rset.next()) {
				degree = new DegreeForm();
				
				string_val = StudentModel.getString(StudentModel.StringToID.UNIVERSITY, 
						rset.getInt("university"));
				degree.setUniversity(string_val);
				
				string_id = StudentModel.getUnivLocID(StudentModel.StringToID.UNIVERSITY, 
						string_val);
				string_val = StudentModel.getString(StudentModel.StringToID.LOCATION, 
						string_id);
				degree.setLocation(string_val);
				
				string_val = StudentModel.getString(StudentModel.StringToID.UNIVERSITY, 
						rset.getInt("discipline"));
				degree.setMajor(string_val);
				degree.setMonth(String.valueOf(rset.getInt("grad_month")));
				degree.setYear(String.valueOf(rset.getInt("grad_year")));
				degree.setGpa(String.valueOf(rset.getFloat("gpa")));
				degree.setTitle(rset.getString("title"));
				
				transcript = rset.getBinaryStream("transcript");
				degree.setFilename("transcript.pdf");
				try {
					degree.setFilesize(String.valueOf(transcript.available()));
				} catch (IOException e) {
					System.err.println("IOException: " + e.getMessage());
				}
				
				if(DEBUG) {
					System.err.println("StudentAppAction: degree found");
				}
				
				degrees.add(degree);
			}
		} catch (SQLException e) {
			System.err.println("SQLException: " + e.getMessage());
		}
		
		request.getSession().setAttribute("fname", fname);
		request.getSession().setAttribute("lname", lname);
		request.getSession().setAttribute("mname", mname);
		request.getSession().setAttribute("citizen", citizen);
		request.getSession().setAttribute("residence", residence);
		request.getSession().setAttribute("address", address);
		request.getSession().setAttribute("city", city);
		request.getSession().setAttribute("state", state);
		request.getSession().setAttribute("zip_code", zip_code);
		request.getSession().setAttribute("country_code", country_code);
		request.getSession().setAttribute("area_code", area_code);
		request.getSession().setAttribute("telephone", telephone);
		request.getSession().setAttribute("residency", residency);
		request.getSession().setAttribute("specialization", specialization);
		request.getSession().setAttribute("admission", admission);
		
		request.getSession().setAttribute("degrees", degrees);
		
		return mapping.findForward("success");
	}
}
